<template>
  <view class="container">
    <!-- 用户信息 -->
    <view
      v-if="isAuthInfo"
      class="userinfo"
    >
      <view class="userinfo-con">
        <view class="userinfo-avatar">
          <image
            :src="
              info.pic
                ?
                  (info.pic.indexOf('http') === -1 ? picDomain + info.pic : info.pic)
                :
                  '/static/images/icon/head04.png'
            "
          />
        </view>
        <view class="userinfo-name">
          <view>{{ info.nickName ? info.nickName : "用户昵称" }}</view>
        </view>
      </view>
      <view class="tip-bnt" @tap="cashDetail">资金明细</view>
    </view>


    <view
      v-if="!isAuthInfo"
      class="userinfo-none"
    >
      <view
        class="default-pic"
        @tap="toLogin"
      >
        <image src="@/static/images/icon/head04.png" />
      </view>
      <view
        class="none-login"
        @tap="toLogin"
      >
        <button class="unlogin">
          未登录
        </button>
        <button class="click-login">
          点击登录账号
        </button>
      </view>
    </view>
    <!-- end 用户信息 -->

    <view class="list-cont">
      <!-- 订单状态 -->
      <view class="total-order">
        <view class="order-tit">
          <text style="font-weight:bold">
            我的订单
          </text>
          <view
            class="checkmore"
            data-sts="0"
            @tap="toOrderListPage"
          >
            <text>查看全部</text>
            <text class="arrowhead" />
          </view>
        </view>
        <view class="procedure">
          <view
            class="items"
            data-sts="1"
            @tap="toOrderListPage"
          >
            <image src="@/static/images/icon/toPay.png" />
            <text>待支付</text>
            <text
              v-if="orderAmount.unPay>0"
              class="num-badge"
            >
              {{ orderAmount.unPay }}
            </text>
          </view>
          <view
            class="items"
            data-sts="2"
            @tap="toOrderListPage"
          >
            <image src="@/static/images/icon/toDelivery.png" />
            <text>待发货</text>
            <text
              v-if="orderAmount.payed>0"
              class="num-badge"
            >
              {{ orderAmount.payed }}
            </text>
          </view>
          <view
            class="items"
            data-sts="3"
            @tap="toOrderListPage"
          >
            <image src="@/static/images/icon/toTake.png" />
            <text>待收货</text>
            <text
              v-if="orderAmount.consignment>0"
              class="num-badge"
            >
              {{ orderAmount.consignment }}
            </text>
          </view>
          <view
            class="items"
            data-sts="5"
            @tap="toOrderListPage"
          >
            <image src="@/static/images/icon/toComment.png" />
            <text>已完成</text>
          </view>
          <view style="margin-top: 2px;"
            class="items"
            @tap="toOrderListReturn"
          >
            <image src="@/static/images/icon/return.png" style="width: 54rpx;height: 54rpx;margin-bottom: 30rpx;" mode="widthFix"/>
            <text>已退款</text>
          </view>
        </view>
      </view>
      <!--end 订单状态 -->

      <view class="prod-col">
        <view
          class="col-item"
          @tap="myCollectionHandle"
        >
          <view
            v-if="loginResult"
            class="num"
          >
            {{ collectionCount }}
          </view>
          <view
            v-else
            class="num"
          >
            --
          </view>
          <view class="tit">
            我的收藏
          </view>
        </view>
        <view
          class="col-item"

        >
        <!-- @tap="handleTips" -->
          <view
            v-if="loginResult"
            class="num"
            style="position: relative;"
          >
           {{PeopleNumber}}
           <!-- -->
           <span class="tip-bnt"  @tap="showPopup" v-if="(PeopleNumber>num||PeopleNumber>num1)&isleader===0">升级</span>
          </view>
          <view
            v-else
            class="num"
          >
            --
          </view>
          <view class="tit">
            <span v-if="isleader==0">我的推荐</span>
            <span v-else>我的团员</span>
          </view>
        </view>
        <view
          class="col-item"
          @tap="handleTips"
        >
          <view
            v-if="loginResult"
            class="num"
          >
            3
          </view>
          <view
            v-else
            class="num"
          >
            --
          </view>
          <view class="tit">
            我的足迹
          </view>
        </view>
      </view>
      <view class="prod-col1" style="margin-top:20rpx;" v-if="isleader==1">
        <view class="title">昨日奖励 <span style="color: #999;float: right;">{{date}}</span> </view>
        <view class="prod-col " style="margin-top: 0;">
          <view
            class="col-item"
          >
          <!-- @tap="handleTips" -->
            <view
              v-if="loginResult"
              class="num"
            >
             {{ yestdayOrderNum }}
            </view>
            <view
              v-else
              class="num"
            >
              --
            </view>
            <view class="tit">
              收货数量
            </view>
          </view>
          <view
            class="col-item"
            @tap="handleTips"
          >
            <view v-if="loginResult"

              class="num"
            >
              {{yestdayMoney}}
            </view>
            <view
              v-else
              class="num"
            >
              --
            </view>
            <view class="tit">
              昨日收益
            </view>
          </view>
        </view>

      </view>

      <view class="my-menu">
        <view
          class="memu-item"
          @tap="toDistCenter"
        >
          <view class="i-name">
            <image src="@/static/images/icon/promotion.png" />
            <text>分销中心</text>
          </view>
          <view class="arrowhead" />
        </view>
        <view
          class="memu-item"
          @tap="toApply"
        >
          <view class="i-name">
            <image src="@/static/images/icon/promotion.png" />
            <text>提现申请</text>
          </view>
          <view class="arrowhead" />
        </view>
       <!-- <view
          class="memu-item"
          @tap="toCouponCenter"
        >
          <view class="i-name">
            <image src="@/static/images/icon/getCoupon.png" />
            <text>领券中心</text>
          </view>
          <view class="arrowhead" />
        </view> -->
      <!--  <view
          class="memu-item"
          @tap="toMyCouponPage"
        >
          <view class="i-name">
            <image src="@/static/images/icon/myCoupon.png" />
            <text>我的优惠券</text>
          </view>
          <view class="arrowhead" />
        </view> -->
        <view
            class="memu-item"
            @tap="toSuggest"
          >
            <view class="i-name">
              <image src="@/static/images/icon/myCoupon.png" />
              <text>投诉建议</text>
            </view>
            <view class="arrowhead" />
        </view>
        <view
            class="memu-item"
            @tap="toSuggestList"
          >
            <view class="i-name">
              <image src="@/static/images/icon/myCoupon.png" />
              <text>我的建议</text>
            </view>
            <view class="arrowhead" />
        </view>
        <view
          class="memu-item"
          @tap="toAddressList"
        >
          <view class="i-name">
            <image src="@/static/images/icon/myAddr.png" />
            <text>收货地址</text>
          </view>
          <view class="arrowhead" />
        </view>
      </view>
      <!--end 列表项 -->

      <view
        v-if="isAuthInfo"
        class="log-out"
        @tap="logout"
      >
        <view class="log-out-n">
          <text>退出登录</text>
        </view>
      </view>
    </view>
      <view class="popup" v-if="popupShow">
        <view class="mask"></view>
        <view class="popup-item">
          <view class="inputView"><input type="text" v-model="teamName" placeholder="请输入团队名称"></view>
          <view class="handle">
            <view @tap="cancel">取消</view>
            <view @tap="upLevel" style="background-color: #3a86b9;color: #fff;">确定</view>
          </view>
        </view>
      </view>
  </view>
</template>

<script setup>
const picDomain = import.meta.env.VITE_APP_RESOURCES_URL
const popupShow=ref(false)
const isAuthInfo = ref(false)
const loginResult = ref('')
const info = ref({})
const orderAmount = ref('')
const PeopleNumber= ref('')
const num=ref('')
const num1=ref('')
const yestdayOrderNum=ref('')
const yestdayMoney=ref('')
const date=ref('')
const isleader=ref('')
const teamName=ref('')
/**
 * 生命周期函数--监听页面显示
 */
onLoad(() => {
  getInfo()
})
onShow(() => {
  loginResult.value = uni.getStorageSync('loginResult')
  let t=uni.getStorageSync('info').userLevel//0 普通团员 1 团长
  isleader.value=t
  if(isleader.value==1){
    getYestdayNum()
  }

  isAuthInfo.value = !!loginResult.value
  // 加载订单数字
  if (isAuthInfo.value) {
    uni.showLoading()
    http.request({
      url: '/p/myOrder/orderCount',
      method: 'GET',
      data: {}
    })
      .then(({ data }) => {
        uni.hideLoading()
        orderAmount.value = data
      })
    showCollectionCount()
    getPeopleNumber()

    getKey('upgrade_leader_direct_push_number')
     getKey('upgrade_leader_lower_level_number')
  }
})
const showPopup=()=>{
  popupShow.value=true
}
const cancel=()=>{
  popupShow.value=false
}
const upLevel=() =>{
  http.request({
    url: '/p/user/upgrade?teamName='+teamName.value,
    method: 'POST',

    data: {  }
  }).then(({ data }) => {
       uni.showToast({
         title:"提交成功"
       })
      popupShow.value=false
  })
}
const getPeopleNumber =()=>{
  http.request({
    url: '/p/user/getLevel1SharedPeopleNumber',
    method: 'GET',
    data: {}
  }).then(({ data }) => {
     PeopleNumber.value = data.lowerLevelNumber
    })
}
const getInfo =()=>{
  http.request({
    url: '/p/user/userInfo',
    method: 'GET',
    data: {}
  })
    .then(({ data }) => {
      info.value = data
      uni.setStorageSync('info',info.value)
    })
}
const getKey = (key) => {
   http.request({
     url: '/p/user/getConfigByKey',
     method: 'GET',
     data: {
       key:key
     }
   })
     .then(({ data }) => {
       if(key=='upgrade_leader_direct_push_number'){
         num.value = data
       }else if(key=='upgrade_leader_lower_level_number'){
         num1.value = data
       }

     })

}
const getYestdayNum = () => {
   http.request({
     url: '/p/oneLevelDistribution/getTeamLeaderBonus',
     method: 'GET',
     data: {}
   })
     .then(({ data }) => {

       let i=data.confirmReceiptNum
       yestdayOrderNum.value=i
       yestdayMoney.value=data.rewardAmount
       date.value=data.date
       console.log(yestdayOrderNum)
     })

}
const toDistCenter = () => {
  uni.navigateTo({
    url: '/pages/user/distCenter/distCenter'
  })
}
const toSuggest = () => {
  uni.navigateTo({
    url: '/pages/user/suggest/suggest'
  })
}
const toSuggestList = () => {
  uni.navigateTo({
    url: '/pages/user/suggest/list'
  })
}
const toApply = () => {
  uni.navigateTo({
    url: '/pages/cash/apply'
  })
}

const toCouponCenter = () => {
  uni.showToast({
    icon: 'none',
    title: '该功能未开源'
  })
}
const toMyCouponPage = () => {
  uni.showToast({
    icon: 'none',
    title: '该功能未开源'
  })
}
const cashDetail = () => {
  uni.navigateTo({
    url: '/pages/cash/cash'
  })
}
const handleTips = () => {
  uni.showToast({
    icon: 'none',
    title: '该功能未开源'
  })
}
const toAddressList = () => {
  uni.navigateTo({
    url: '/pages/delivery-address/delivery-address'
  })
}

const toOrderListPage = (e) => {
  const sts = e.currentTarget.dataset.sts
  uni.navigateTo({
    url: '/pages/orderList/orderList?sts=' + sts
  })
}
const toOrderListReturn = (e) => {
  uni.navigateTo({
    url: '/pages/orderList/returnList'
  })
}
const collectionCount = ref(0)
/**
 * 查询所有的收藏量
 */
const showCollectionCount = () => {
  uni.showLoading()
  http.request({
    url: '/p/user/collection/count',
    method: 'GET',
    data: {}
  })
    .then(({ data }) => {
      uni.hideLoading()
      console.log(data)
      collectionCount.value = data
    })
}

/**
 * 我的收藏跳转
 */
const myCollectionHandle = () => {
  let url = '/pages/prod-classify/prod-classify?sts=5'
  const id = 0
  const title = '我的收藏商品'
  if (id) {
    url += '&tagid=' + id + '&title=' + title
  }
  uni.navigateTo({
    url
  })
}

/**
 * 去登陆
 */
const toLogin = () => {
  uni.navigateTo({
    url: '/pages/accountLogin/accountLogin'
  })
}

/**
 * 退出登录
 */
const logout = () => {
  http.request({
    url: '/logOut',
    method: 'post'
  })
    .then(() => {
      util.removeTabBadge()
      uni.removeStorageSync('loginResult')
      uni.removeStorageSync('token')
      uni.showToast({
        title: '退出成功',
        icon: 'none'
      })
      orderAmount.value = ''
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }, 1000)
    })
}
</script>

<style scoped lang="scss">
@use './user.scss';
.prod-col1{
  background-color: #fff;

}
.title{
  font-size: 26rpx;
  padding: 30rpx 50rpx 10rpx;
  background-color: #fff;

}
.tip-bnt{
  position: absolute;
  top: -5px;
  right: -10px;
  font-size: 22rpx;
  display: inline-block;
  padding: 7rpx 11rpx;
  background: #f9ba09;
  border-radius: 12rpx;
  color: #fff;
}
</style>
